<?php

/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/Project for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */
namespace Project\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
// use Project\Model\Project;
use Project\Model\projectviewTable;
use Project\Model\projectview;
use Project\Model\Project;
use Project\Model\ProjectTable; // <-- Add this import
use Project\Form\ProjectForm;
use Project\Model\Task;
use Project\Form\TaskForm;

class ProjectController extends AbstractActionController {
	protected $projectviewTable;
	protected $projectTable;
	protected $taskTable;
	public function indexAction() {
		return new ViewModel ( array (
				'projectviews' => $this->getprojectviewTable ()->fetchAll () 
		) );
	}
	public function getprojectviewTable() {
		if (! $this->projectviewTable) {
			$sm = $this->getServiceLocator ();
			$this->projectviewTable = $sm->get ( 'Project\Model\projectviewTable' );
		}
		return $this->projectviewTable;
	}
	public function getProjectTable() {
		if (! $this->projectTable) {
			$sm = $this->getServiceLocator ();
			$this->projectTable = $sm->get ( 'Project\Model\projectTable' );
		}
		return $this->projectTable;
	}
	public function getTaskTable() {
		if (! $this->taskTable) {
			$sm = $this->getServiceLocator ();
			$this->taskTable = $sm->get ( 'Project\Model\TaskTable' );
		}
		return $this->taskTable;
	}
	public function addAction() {
		$form = new ProjectForm ();
		$form->get ( 'submit' )->setValue ( 'Add' );
		
		$request = $this->getRequest ();
		if ($request->isPost ()) {
			$project = new Project ();
			$form->setInputFilter ( $project->getInputFilter () );
			$form->setData ( $request->getPost () );
			
			if ($form->isValid ()) {
				$project->exchangeArray ( $form->getData () );
				$this->getProjectTable ()->saveProject ( $project );
				
				// Redirect to list of albums
				return $this->redirect ()->toRoute ( 'project' );
			}
		}
		return array (
				'form' => $form 
		);
	}
	public function addtaskAction() {
		$form = new TaskForm ();
		$form->get ( 'submit' )->setValue ( 'addTask' );
		
		$request = $this->getRequest ();
		if ($request->isPost ()) {
			$task = new Task ();
			$form->setInputFilter ( $task->getInputFilter () );
			$form->setData ( $request->getPost () );
			
			if ($form->isValid ()) {
				$task->exchangeArray ( $form->getData () );
				$this->getTaskTable ()->saveTask ( $task );
				
				// Redirect to list of albums
				return $this->redirect ()->toRoute ( 'project' );
			}
		}
		return array (
				'form' => $form 
		);
	}
	public function editAction() {
		$id = ( int ) $this->params ()->fromRoute ( 'id', 0 );
		if (! $id) {
			return $this->redirect ()->toRoute ( 'project', array (
					'action' => 'add' 
			) );
		}
		$project = $this->getProjectTable ()->getProject ( $id );
		
		$form = new ProjectForm ();
		$form->bind ( $project );
		$form->get ( 'submit' )->setAttribute ( 'value', 'Edit' );
		
		$request = $this->getRequest ();
		if ($request->isPost ()) {
			$form->setInputFilter ( $project->getInputFilter () );
			$form->setData ( $request->getPost () );
			
			if ($form->isValid ()) {
				$this->getProjectTable ()->saveProject ( $form->getData () );
				
				// Redirect to list of albums
				return $this->redirect ()->toRoute ( 'project' );
			}
		}
		return array (
				'id' => $id,
				'form' => $form 
		);
	}
	public function deleteAction() {
		$id = ( int ) $this->params ()->fromRoute ( 'id', 0 );
		if (! $id) {
			return $this->redirect ()->toRoute ( 'project' );
		}
		
		$request = $this->getRequest ();
		if ($request->isPost ()) {
			$del = $request->getPost ( 'del', 'No' );
			
			if ($del == 'Yes') {
				$id = ( int ) $request->getPost ( 'id' );
				$this->getProjectTable ()->deleteProject ( $id );
			}
			
			// Redirect to list of albums
			return $this->redirect ()->toRoute ( 'project' );
		}
		
		return array (
				'id' => $id,
				'project' => $this->getProjectTable ()->getProject ( $id ) 
		);
	}
	public function fooAction() {
		// This shows the :controller and :action parameters in default route
		// are working when you browse to /project/project/foo
		return array ();
	}
}
